package com.hua.api.core.framework.config.scurity;

import cn.hutool.crypto.SecureUtil;
import com.hua.api.core.framework.util.DateUtil;
import com.hua.api.core.framework.util.exception.ApiError;
import com.hua.api.core.util.SpringContextHolder;
import com.hua.api.core.constants.Status;
import com.hua.api.core.util.StringUtil;
import com.hua.api.modules.home.domain.OpenClient;
import com.hua.api.modules.home.service.OpenClientService;

import java.util.*;

public class SignUtil {

    public static Map<String,OpenClient> openClients = new HashMap<>();

    public static String dateHour = "";

    public static OpenClient fetchAppScrect( String appKey){

        String now = DateUtil.getFormatDate(new Date(),"yyyyMMddHH");

        if(openClients.containsKey(appKey)){
            return openClients.get(appKey);
        }else{

            OpenClientService openClientService = SpringContextHolder.getBean(OpenClientService.class);
            OpenClient client = openClientService.findOpenClientUseAppkey(appKey);
            if(null!=client){
                openClients.put(appKey,client);
                return client;
            }

        }
        return null;
    }

    public static String signUseAppSecret(Map<String, String> params, String appSecret){

        SortedMap<String, String> treeMap =  new TreeMap<>();
        for(Map.Entry<String, String> en:params.entrySet()){
            treeMap.put(en.getKey(),en.getValue());
        }
        String str = toSignString(treeMap);
        String src = str+appSecret;
        return SecureUtil.md5(src);
    }


    public static String sign(Map<String, String> params, String appKey){

        OpenClient client = fetchAppScrect(appKey);

        if(null==client){
            throw ApiError.Type.TOKEN_ERROR.toException("appKey不存在！");
        }

        if(!StringUtil.equals(Status.LOGIC_TRUE, client.getState())){
            throw ApiError.Type.TOKEN_ERROR.toException("状态异常！");
        }


        SortedMap<String, String> treeMap =  new TreeMap<>();
        for(Map.Entry<String, String> en:params.entrySet()){
            treeMap.put(en.getKey(),en.getValue());
            // treeMap.put(entry.getKey(),entry.getValue());
        }
        String str = toSignString(treeMap);
        String src = str+client.getAppScrect();
        return SecureUtil.md5(src);
    }

    private static String toSignString(SortedMap<String, String> map){
        StringBuffer sbuf = new StringBuffer();
        boolean b = true;
        for(Map.Entry<String, String> en:map.entrySet()){
            if(!StringUtil.isEmpty(en.getValue()) && !"key".equals(en.getKey())){
                sbuf.append((b?"":"&")+en.getKey()+"="+en.getValue());
                if(b){b=false;}
            }
        }
        return sbuf.toString();
    }


//    public static void main(String[] args){
//        Map<String,String> params = new HashMap<>();
//
//        params.put(TokenHelper.Hua_User_Agent,"createHuawaOrder");
//        params.put(TokenHelper.Hua_Appkey,"300075430127");
//        params.put(TokenHelper.Hua_Timestame,"1736930704536");
//        params.put(TokenHelper.Hua_Nonce,"eNPlhliEzVLF1RaB5ktp");
//        System.out.println(signUseAppSecret(params,"pyElfsfoSf94iQK72npGFtFG"));
//
//    }

}
